package com.appcenter.task.service;

import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.jz.unit.ObjectUnit;
import com.appcenter.task.input.SearchAlreadyTaskListInput;
import com.appcenter.task.input.SearchUnTaskListInput;
import com.appcenter.task.input.externalapi.*;
import com.appcenter.task.mapper.AlreadyTasksMapper;
import com.appcenter.task.mapper.UnTaskHistorysMapper;
import com.appcenter.task.mapper.UnTasksMapper;
import com.appcenter.task.model.AlreadyTasks;
import com.appcenter.task.model.UnTaskHistorys;
import com.appcenter.task.model.UnTasks;
import com.appcenter.task.output.SearchAlreadyTaskListOutput;
import com.appcenter.task.output.SearchUnTaskListOutput;
import com.appcenter.task.output.externalapi.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static org.apache.http.util.TextUtils.isBlank;

@Service
public class ExternalAPISercive {

    @Autowired
    UnTasksMapper unTasksMapper;
    @Autowired
    UnTaskHistorysMapper unTaskHistorysMapper;
    @Autowired
    AlreadyTasksMapper alreadyTasksMapper;
    @Autowired
    UnTaskService unTaskService;

    @Autowired
    AlreadyTaskService alreadyTaskService;

    /**
     *  创建人:Coke
     *  备注: 推送待办
     *  时间: 2019/4/26 14:48 
     *  参数: 
     */
//    @Transactional
    public SendTaskOutput SendTask(SendTaskInput input) throws Exception {
        return unTaskService.SendTask(input);
    }
    /**
     *  创建人:Coke
     *  备注: 待办转已办
     *  时间: 2019/4/26 14:48 
     *  参数: 
     */
//    @Transactional
    public ConversionTaskOutput ConversionTask(ConversionTaskInput input) throws Exception {
        if (isBlank(input.getAlreadyHandleUserId())) {
            throw new ErrorJsonResult("10000031", "办理人Id不能为空");
        }
        if (isBlank(input.getAlreadyHandleUserCode())) {
            throw new ErrorJsonResult("10000031", "办理人Code不能为空");
        }
        if (isBlank(input.getAlreadyHandleUserName())) {
            throw new ErrorJsonResult("10000031", "办理人Code不能为空");
        }
        input.setAlreadyGenre("正常办结");
        return unTaskService.ConversionTask(input);
    }

    /**
     *  创建人:Coke
     *  备注: 查询待办信息, 列表
     *  时间: 2019/4/26 14:48 
     *  参数: 
     */
    public SearchUnTaskListOutput GetUnTasks(SearchUnTaskListInput input) {
        if (isBlank(input.getUserCode())) {
            throw new ErrorJsonResult("1000031", "未找到接收人信息,请重试");
        }
        return unTaskService.SearchUnTaskList(input);
    }

    /**
     *  创建人:Coke
     *  备注: 查询待办信息, 单条
     *  时间: 2019/4/26 14:48 
     *  参数: 
     */
    public GetUnTaskOutput GetUnTask(GetUnTaskInput input) {
        GetUnTaskOutput output = new GetUnTaskOutput();
        output.setTask(unTasksMapper.selectByPrimaryKey(input.getTaskId()));
        return output;
    }

    /**
     *  创建人:Coke
     *  备注: 查询已办信息, 列表
     *  时间: 2019/4/26 14:48 
     *  参数: 
     */
    public SearchAlreadyTaskListOutput GetAlreadys(SearchAlreadyTaskListInput input) {
        if (isBlank(input.getUserCode())) {
            throw new ErrorJsonResult("1000031", "未找到接收人信息,请重试");
        }
        return alreadyTaskService.SearchAlreadyTaskList(input);
    }

    /**
     *  创建人:Coke
     *  备注: 查询已办信息, 单条
     *  时间: 2019/4/26 14:53
     *  参数: 
     */
    public GetAlreadyOutput GetAlready(GetAlreadyInput input) throws Exception {
        GetAlreadyOutput output = new GetAlreadyOutput();
        AlreadyTasks curAlreadyTask = new AlreadyTasks();
        curAlreadyTask = alreadyTasksMapper.selectByPrimaryKey(input.getAlreadyTaskId());
        ObjectUnit.Copy(curAlreadyTask, output);
        if (curAlreadyTask == null) {
            throw new ErrorJsonResult("1000031", "未找到已办信息");
        }
        if (isBlank(curAlreadyTask.getUnTaskId())) {
            throw new ErrorJsonResult("1000031", "该已办信息未找到源待办信息");
        }
        UnTaskHistorys curUnTaskHistorys = unTaskHistorysMapper.selectByPrimaryKey(curAlreadyTask.getUnTaskId());
        output.setCurTask(curUnTaskHistorys);
        return output;
    }


}
